#include "atmoslayer.h"
#include <math.h>
#include <QDebug>

#define EXCLUDE_FORWARD_SCAT
void AtmosLayer::downIrradianceCaculate()
{
	qDebug() << "caculating downward irradiance.";

	double scatIrradiance = 0; //sum of scatter light
	m_currentIrradiance = 0; //must clear for current caculate

	//**************first add up downward-scatter light
	double zenith, cosZen;
	int i;
	for(i = 0, zenith = M_PI - ZENITH_INIT;
	    i < N_PI/2; i++, zenith -= ANGLE_STEP){

		cosZen = qAbs(cos(zenith));
		for(int j = 0; j < N_PI*2; j++){
		    scatIrradiance += m_lightScatD[i][j][0] *
			    m_solidAngle[i] * cosZen;
		}
	}
	//Pi*F = Int I*u dw, F = 1/Pi Int I*u dw.
	m_currentIrradiance += scatIrradiance / M_PI;
	m_downIrradiance += scatIrradiance / M_PI;

	//**************then add the solar direct flux
	if(m_solarOut != 0){
		//solar flux must moduled by inclined parameter mu.
		m_solarIrradiance = m_solarOut * qAbs(cos(m_solarZen));
		m_downIrradiance += m_solarIrradiance;
		m_currentIrradiance += m_solarIrradiance;
	}
	qDebug() << "***************************\n" << m_layerName << ":\n\twhole downward irradiance" << m_downIrradiance << ", diff-whole-ratio: " << (m_downIrradiance - m_solarIrradiance)/m_downIrradiance << "\n\tdamped solar irradiance(R0*mu)" << m_solarIrradiance << "\n\tcurrent caculating Irradiance" << m_currentIrradiance;

	emit(gotDownIrradiance(m_currentIrradiance));
}
